package com.leetcode.weekly.contest.no235th;

import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author 覃国强
 * @date 2021-04-04 11:03
 */
public class A02_查找用户活跃分钟数 {

  public static void main(String[] args) {
    Solution solution = new Solution();
    int[][] logs = {
        {1, 1},
        {2, 2},
        {2, 3}
    };
    int k = 4;
    int[] answer = solution.findingUsersActiveMinutes(logs, k);
    System.out.println(Arrays.toString(answer));
  }


  static
  class Solution {
    public int[] findingUsersActiveMinutes(int[][] logs, int k) {
      Set<UserLog> set = new TreeSet<>();
      for (int[] log : logs) {
        set.add(new UserLog(log[0], log[1]));
      }
      int[] answer = new int[k];
      UserLog userLog = null;
      int count = 0;
      for (UserLog log : set) {
        if (userLog == null || userLog.id == log.id) {
          ++count;
        } else {
          ++answer[count - 1];
          count = 1;
        }
        userLog = log;
      }
      ++answer[count - 1];
      return answer;
    }


    private static class UserLog implements Comparable<UserLog> {

      int id;

      int time;

      public UserLog(int id, int time) {
        this.id = id;
        this.time = time;
      }

      @Override
      public boolean equals(Object o) {
        if (this == o) {
          return true;
        }
        if (o == null || getClass() != o.getClass()) {
          return false;
        }
        UserLog userLog = (UserLog) o;
        return id == userLog.id && time == userLog.time;
      }

      @Override
      public int hashCode() {
        return Objects.hash(id, time);
      }

      @Override
      public int compareTo(UserLog o) {
        int tmp = this.id - o.id;
        if (tmp == 0) {
          return this.time - o.time;
        }
        return tmp;
      }
    }

  }

}
